const {
  faceGroupList,
  facePersonList,
  faceAddPerson,
  faceSearch,
} = require('../face-api/tencentFace')
class TencentController {
  async getGroupList(ctx, next) {
    const result = await faceGroupList()
    ctx.body = result
  }
  async getPersonList(ctx, next) {
    const result = await facePersonList()
    ctx.body = result
  }
  async addPerson(ctx, next) {
    let data = {}
    if (ctx.request.img) {
      let Image = ctx.request.img
      data = {
        Image,
        ...ctx.request.body,
      }
    } else {
      let Image = ctx.request.body.image
      let PersonName = ctx.request.body.PersonName
      data = {
        Image,
        PersonName
      }
    }
    // const Image = ctx.request.img
    let userId = 'user1'
    const userListInfo = await facePersonList()
    const userList = userListInfo.PersonInfos
    if (userList.length !== 0) {
      const num = userList[0].PersonId.replace('user', '')
      userId = `user${parseInt(num) + 1}`
    }
    // const data = {
    //   Image,
    //   ...ctx.request.body,
    //   PersonId: userId,
    // }
    data.PersonId = userId
    const result = await faceAddPerson(data)
    // ctx.body = result
    if (typeof result === 'string') {
      ctx.body = {
        status: 412,
        message:result
      }
    } else {
      ctx.body = result
    }
  }
  async search(ctx, next) {
    let data = {}
    if (ctx.request.img) {
      let Image = ctx.request.img
      data = {
        Image,
        ...ctx.request.body,
      }
    } else {
      let Image = ctx.request.body.image
      let FaceMatchThreshold = ctx.request.body.FaceMatchThreshold
      data = {
        Image,
        FaceMatchThreshold
      }
    }
    // const Image = ctx.request.img ? ctx.request.img : ctx.request.body.image

    const result = await faceSearch(data)
    console.log('result',result)
    if (typeof result === 'string') {
      ctx.body = {
        status: 412,
        message:result
      }
    } else {
      ctx.body = result
    }
  }
}

module.exports = new TencentController()
